package com.liit.controller.func;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liit.annotation.SystemLog;
import com.liit.controller.index.BaseController;
import com.liit.entity.ProvinceFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.entity.WxuserGroupFormMap;
import com.liit.exception.SystemException;
import com.liit.mapper.WeixinUserMapper;
import com.liit.plugin.PageView;
import com.liit.util.Common;

/**
 * @author dingweibo
 * @Title: WeixinUserController.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 2016年1月26日
 */
@Controller
@RequestMapping("/wxuser/")
public class WeixinUserController extends BaseController {

	@Inject
	private WeixinUserMapper weixinUserMapper;

	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		model.addAttribute("group", weixinUserMapper.findByWhere(new WxuserGroupFormMap()));
		return Common.BACKGROUND_PATH + "/func/listWeixinUser";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		WeixinUserFormMap weixinUserFormMap = getFormMap(WeixinUserFormMap.class);
		weixinUserFormMap = toFormMap(weixinUserFormMap, pageNow, pageSize, weixinUserFormMap.getStr("orderby"));
		pageView.setRecords(weixinUserMapper.findByPage(weixinUserFormMap));
		return pageView;
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			model.addAttribute("weixinUser", weixinUserMapper.findbyFrist("id", id, WeixinUserFormMap.class));
		}
		model.addAttribute("group", weixinUserMapper.findByWhere(new WxuserGroupFormMap()));
		return Common.BACKGROUND_PATH + "/func/editWeixinUser";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "功能管理", methods = "关注用户-修改用户信息")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		WeixinUserFormMap weixinUserFormMap = getFormMap(WeixinUserFormMap.class);
		weixinUserMapper.editEntity(weixinUserFormMap);
		return "success";
	}

	@RequestMapping("viewUI")
	public String viewUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			model.addAttribute("weixinUser", weixinUserMapper.findbyFrist("id", id, WeixinUserFormMap.class));
		}
		model.addAttribute("group", weixinUserMapper.findByWhere(new WxuserGroupFormMap()));
		return Common.BACKGROUND_PATH + "/func/viewWeixinUser";
	}

	@RequestMapping("listReg")
	public String listReg(Model model) throws Exception {
		listUI(model);
		return Common.BACKGROUND_PATH + "/func/listWeixinRegUser";
	}

	@RequestMapping("editRegUI")
	public String editRegUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			model.addAttribute("weixinUser", weixinUserMapper.findbyFrist("id", id, WeixinUserFormMap.class));
		}
		model.addAttribute("province", weixinUserMapper.findByWhere(new ProvinceFormMap()));
		model.addAttribute("group", weixinUserMapper.findByWhere(new WxuserGroupFormMap()));
		return Common.BACKGROUND_PATH + "/func/editWeixinRegUser";
	}

	@RequestMapping("addRegUI")
	public String addRegUI(Model model) throws Exception {
		model.addAttribute("province", weixinUserMapper.findByWhere(new ProvinceFormMap()));
		model.addAttribute("group", weixinUserMapper.findByWhere(new WxuserGroupFormMap()));
		return Common.BACKGROUND_PATH + "/func/addWeixinRegUser";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@SystemLog(module = "功能管理", methods = "注册用户-新增用户")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	public String addEntity() {
		try {
			WeixinUserFormMap weixinUserFormMap = getFormMap(WeixinUserFormMap.class);
			weixinUserMapper.addEntity(weixinUserFormMap);
		} catch (Exception e) {
			throw new SystemException("新增用户异常");
		}
		return "success";
	}

	@RequestMapping("editBatchUI")
	public String editBatchUI(Model model) throws Exception {
		model.addAttribute("ids", getPara("ids").split(","));
		model.addAttribute("group", weixinUserMapper.findByWhere(new WxuserGroupFormMap()));
		return Common.BACKGROUND_PATH + "/func/editBatchWeixinUser";
	}

	@ResponseBody
	@RequestMapping("editBatch")
	@SystemLog(module = "功能管理", methods = "关注用户-批量修改分组")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional(readOnly = false)
	public String editBatch(Model model) throws Exception {
		String[] ids = getParaValues("weixinUserFormMap.id");
		String groupId = getPara("weixinUserFormMap.groupId");
		WeixinUserFormMap record = new WeixinUserFormMap();
		record.put("groupId", groupId);
		for (String id : ids) {
			record.put("id", id);
			weixinUserMapper.editEntity(record);
		}
		return "success";
	}
}
